package org.robert.export_client.excel.builder;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.CellStyle;
import org.robert.export_client.excel.entity.CellStyleEntity;
import org.robert.export_client.excel.entity.WriteSheet;
import org.robert.export_client.excel.impl.ExcelHeadService;
import org.robert.export_client.excel.impl.ExcelWriteByDataService;
import org.robert.export_client.excel.metadata.AbstractSheetBuilder;
import org.robert.export_client.excel.metadata.AbstractWorkbookBuilder;
import org.robert.export_client.excel.metadata.AbstractWriteSheet;
import org.robert.export_client.excel.metadata.ExcelHeadEntity;
import org.robert.export_client.excel.util.ExcelUtil;

import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author robert
 * @Date 2024/1/20 20:52
 **/
@EqualsAndHashCode(callSuper = true)
public class ExcelSheetDataPathBuilder extends AbstractSheetBuilder {

    private WorkbookDataPathBuilder workbookBuilder;
    public ExcelSheetDataPathBuilder(WorkbookDataPathBuilder workbookBuilder, HSSFSheet sheet, Class<?> clazz,
                                     Map<String, Map<String, String>> dictMap ) throws Exception {
        super(workbookBuilder, sheet, clazz, dictMap);
        this.workbookBuilder = workbookBuilder;
    }

    public ExcelSheetDataPathBuilder(WorkbookDataPathBuilder workbookBuilder, HSSFSheet sheet, Class<?> clazz,
                                Map<String, Map<String, String>> dictMap, CellStyleEntity titleStyle) throws Exception {
        super(workbookBuilder, sheet, clazz, dictMap, titleStyle);
        this.workbookBuilder = workbookBuilder;
    }

    @Override
    public WorkbookDataPathBuilder getWorkbookBuilder() {
        return workbookBuilder;
    }

    public void setWorkbookBuilder(WorkbookDataPathBuilder workbookBuilder) {
        this.workbookBuilder = workbookBuilder;
    }

    @Override
    public void doWrite(List<?> data) throws Exception {
        super.doWrite(data);
        if(flag) {
            this.finish();
        }
    }

    @Override
    public void doWrite(List<?> data, CellStyleEntity cellStyle) throws Exception {
        super.doWrite(data);
        if(this.flag){
            super.finish();

        }
    }

    @Override
    public WriteSheet build(){
        this.flag = false;
        return new WriteSheet(this);
    }
    @Override
    public void finish(){
        super.finish();
        String fileName = this.getWorkbookBuilder().getFileName();
        String fileNameStr = this.getWorkbookBuilder().getPath() + fileName + ".xlsx";
        ExcelUtil.writeWorkbook(this.getWorkbookBuilder().getWorkbook(), fileNameStr);
    }

}
